// 操作数库的对象  db
// 数据库：blog_database
// 数据表：pre_photo

// SELECT  字段
// FROM  数据表
// WHERE  条件
// INSERT  插入
// INTO   数据表
// UPDATE   数据表  更新
// DELETE   删除  
// DELETE FROM 数据表

const db = require("../common/db/index");
// models 目录下的文件负责增删改查
module.exports = {

    // 搜索相册
    search(keyword) {
        return new Promise((resolve, reject) => {
            // 编写sql语句（模糊查询）
            const sql = 'SELECT * FROM pre_photo WHERE photo_title LIKE ?';
            db.query(sql, [`%${keyword}%`])
                .then(
                    result => {
                        if (result.length > 0) {
                            resolve({
                                code: 200,
                                msg: "搜索相册成功",
                                result
                            })
                        } else {
                            resolve({
                                code: -1,
                                msg: "没有搜索到结果",
                                result
                            })
                        }
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "搜索相册失败",
                            err: err
                        })
                    }
                )
        })
    },

    // 发表相册
    publish(photo_title, createAt, photo_url) {
        return new Promise((resolve, reject) => {
            // 编写sql语句（插入数据）
            const sql = 'INSERT INTO pre_photo (photo_title, createAt , photo_url) VALUES (?,?, ?)';
            db.query(sql, [photo_title, createAt, photo_url])
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: "发布成功",
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "发布失败",
                            err: err
                        })
                    }
                )

        })
    },

    // 修改相册
    modify(photo_title, createAt, photo_url, id) {
        return new Promise((resolve, reject) => {
            // 编写sql语句（更新数据）
            const sql = 'UPDATE pre_photo SET photo_title = ?, createAt = ? , photo_url = ? WHERE id = ?';
            db.query(sql, [photo_title, createAt, photo_url, id])
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: "修改成功",
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "修改失败",
                            err: err
                        })
                    }
                )

        })
    },

    // 删除相册
    remove(id) {
        return new Promise((resolve, reject) => {
            // 编写sql语句（更新数据）
            // ? 占位符  和[ ]的参数顺序是一一对应的
            const sql = 'DELETE FROM pre_photo WHERE id = ?';
            db.query(sql, [id])
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: "删除成功",
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "删除失败",
                            err: err
                        })
                    }
                )

        })
    },

    // 获取相册分页列表
    items(page_num, page_size) {
        return new Promise(async (resolve, reject) => {
            const rows = await db.query(`SELECT count(*) as total FROM pre_photo`);
            let page_num2 = ((parseInt(page_num) - 1) * parseInt(page_size))
            let page_size2 = (parseInt(page_size))
            let sql = `select * from pre_photo order by createAt DESC limit ${page_num2} , ${page_size2}`
            await db.query(sql)
                .then(
                    result => {
                        resolve({
                            total: rows[0].total,
                            code: 200,
                            msg: "获取相册列表成功",
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "获取相册列表失败",
                            err
                        })
                    }
                )

        })
    }
}